import Vue from 'vue';
import App from './App.vue';
import router from './router';
import ElementUI from 'element-ui';
import VueI18n from 'vue-i18n';
import {
	messages
} from './components/common/i18n';
import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
//import './assets/css/theme-green/index.css'; // 浅绿色主题
//import './assets/css/theme-green/color-green.css'; // 浅绿色主题
import './assets/css/icon.css';
import './components/common/directives';
import 'babel-polyfill';
import axios from 'axios';
Vue.config.productionTip = false;
Vue.prototype.$http = axios;
Vue.use(VueI18n);
Vue.use(ElementUI, {
	size: 'small'
});
const i18n = new VueI18n({
	locale: 'zh',
	messages
});


// 添加请求拦截器
axios.interceptors.request.use(function (req) {

	let token = localStorage.getItem('token');

	if (token) {
		req.headers.Authorization = "Bearer " + localStorage.getItem("token");
	}
	//config.headers.Authorization = localStorage.getItem("token");
	// console.info(config);
	return req;
}, function (error) {
	return Promise.reject(error);
});

// 响应拦截器
axios.interceptors.response.use(function (res) {
	return res;

}, function (error) {

	
	if (error.response.status == 401) {
		localStorage.removeItem('token');
		router.push('/login');
	}
	if (error.response.status == 403) {
		ElementUI.Message({
			showClose: true,
			message: error.response.data.error.message,
			type: 'warning',
			time: 5000
		});
	}

	if (error.response.status == 999) {//限流

		ElementUI.Message({
			showClose: true,
			message: '您的操作过于频繁，请稍后再试!',
			type: 'warning',
			time: 5000
		});
	}
	return Promise.reject(error);

});
//所有路由，根据权限动态加载
var allRouter = [{
	key: '/user',
	component: () => import('@/views/systemset/user/list.vue'),
}, {
	key: '/menu',
	component: () => import('@/views/systemset/menu/list.vue'),
},
{
	key: '/role',
	component: () => import('@/views/systemset/role/list.vue'),
}, {
	key: '/log',
	component: () => import('@/views/systemset/log/list.vue'),
}, {
	key: '/tenant',
	component: () => import('@/views/systemset/tenant/list.vue'),
},
{
	key: '/skgoods',
	component: () => import('@/views/systemset/menu/list.vue'),
},
{
	key: '/goodscategory',
	component: () => import('@/views/goods/category/list.vue'),
},
{
	key: '/goodsattribute',
	component: () => import('@/views/goods/attribute/list.vue'),
}, {
	key: '/goodsspec',
	component: () => import('@/views/goods/spec/list.vue'),
}, {
	key: '/goodsorder',
	component: () => import('@/views/goods/order/list.vue'),
}, {
	key: '/goods',
	component: () => import('@/views/goods/list/list.vue'),
}, {
	key: '/goodsadd',
	component: () => import('@/views/goods/list/add.vue'),
}, {
	key: '/memberlevel',
	component: () => import('@/views/member/level/list.vue'),
}, {
	key: '/memberlist',
	component: () => import('@/views/member/member/list.vue'),
}, {
	key: '/membercore',
	component: () => import('@/views/member/core/index.vue'),
}, {
	key: '/consumestatistics',
	component: () => import('@/views/statistics/consume.vue'),
}, {
	key: '/integralstatistics',
	component: () => import('@/views/statistics/integral.vue'),
}, {
	key: '/balancestatistics',
	component: () => import('@/views/statistics/balance.vue'),
}, {
	key: '/memberrechargestatistics',
	component: () => import('@/views/statistics/memberrecharge.vue'),
}, {
	key: '/memberbuycardstatistics',
	component: () => import('@/views/statistics/levelupdate.vue'),
}, {
	key: '/wechatincometatistics',
	component: () => import('@/views/statistics/wechatpay.vue'),
}, {
	key: '/alipaystatistics',
	component: () => import('@/views/statistics/alipay.vue'),
}
];

//根据权限生成动态路由
async function GetRouter() {
	let token = localStorage.getItem('token');
	// if (token) {
	// 	await axios({
	// 		//url: 'http://localhost:52295/set/role/getmenubyroleid',
	// 		url: config.gatewayUrl + '/set/role/getmenubyroleid',
	// 		method: 'get'
	// 	}).then((res) => {
	// 		config.menuList = res.data;
	// 		console.log(config.menuList);
	// 	}).catch(() => {
	// 		config.menuList = []
	// 	});
	// }


	var routers = config.menuList.filter(function (item) {
		return item.type != 1;
	});
	var childrenRouter = [
		////必然包含路由，可能框架自带，删除后会有异常
		{
			path: '/dashboard',
			component: () => import('@/components/page/Dashboard.vue'),
			meta: {
				title: '系统首页'
			}
		},
		{
			path: '/tabs',
			component: () => import( /* webpackChunkName: "tabs" */ '@/components/page/Tabs.vue'),
			meta: {
				title: 'tab选项卡'
			}
		}, {
			path: '/goodsadd',
			component: () => import('@/views/goods/list/add.vue'),
			meta: {
				title: '商品编辑'
			}
		}
	];

	for (var i = 0; i < routers.length; i++) {


		var component = allRouter.filter(function (item) {
			return item.key == routers[i].url;
		});

		if (component.length > 0) {
			//var componentUrl = routers[i].path;
			//var s = "@/views/systemset/user/list.vue";
			var r = {
				path: routers[i].url,
				component: component[0].component,
				//component: (resolve) => require([`@/views/${componentUrl}`], resolve),//不行
				//component: () => import('@/views/systemset/user/list.vue'),//行
				//component:()=>import(`@/views/${componentUrl}`),//行
				//component:()=>import(`${s}`),//不行
				meta: {
					title: routers[i].displayName
				}
			};
			childrenRouter.push(r);
		}
	}
	//
	var routerList = [{
		path: '/',
		redirect: '/dashboard'
	},
	{
		path: '/',
		component: () => import( /* webpackChunkName: "home" */ '@/components/common/Home.vue'), //路由懒加载
		meta: {
			title: '自述文件'
		},
		children: childrenRouter //permissionMenu childrenRouter
	},
	{
		path: '/login',
		component: () => import( /* webpackChunkName: "login" */ '@/components/page/Login.vue'),
		meta: {
			title: '登录'
		}
	},
	{
		path: '*',
		redirect: '/404'
	}
	];



	router.addRoutes(routerList);
	Init();

}
GetRouter();

function Init() {

	//https://www.cnblogs.com/woai3c/p/11052975.html
	//使用路由守卫对路由进行权限跳转
	router.beforeEach((to, from, next) => {

		document.title = to.meta.title;
		//document.title = `${to.meta.title}|管理系统`;
		const token = localStorage.getItem('token');

		if (!token && to.path !== '/login') {
			next('/login');
		} else if (to.meta.permission) {
			// 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
			token === 'admin' ? next() : next('/403');
		} else {
			// 简单的判断IE10及以下不进入富文本编辑器，该组件不兼容
			if (navigator.userAgent.indexOf('MSIE') > -1 && to.path === '/editor') {
				Vue.prototype.$alert('vue-quill-editor组件不兼容IE10及以下浏览器，请使用更高版本的浏览器查看', '浏览器不兼容通知', {
					confirmButtonText: '确定'
				});
			} else {
				next();
			}
		}
	});



	new Vue({
		router,
		i18n,
		render: h => h(App)
	}).$mount('#app');
}
